// pages/login/login.js
var http = require("../../utils/http.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 用户名
    userName: '',
    // 密码
    password: '',
    // 是否显示注册
    isRegister: false,

    url1:getApp().globalData.serverURL+'/register',
    url2:getApp().globalData.serverURL+'/login',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      isRegister: options.isRegister == 0
    })
  },
  goBack(){
    wx.switchTab({
      url: '/pages/index/index',
    }) 
  },
  handleChangeShowType() {
    let str =  this.data.isRegister ? '1' : '0'
    wx.redirectTo({
      url: '/pages/login/login?isRegister=' + str
    })
  },
  handleLoginOrRegister() {
    const that = this
    var URL = ""
    if (!this.data.userName.trim()) {
      wx.showToast({
        title: '请输入用户名',
        icon: 'none'
      })
      return
    }
    if (!this.data.password.trim()) {
      wx.showToast({
        title: '请输入密码',
        icon: 'none'
      })
      return
    }
    URL = this.data.isRegister ? that.data.url1 : that.data.url2
    wx.request({
      url: URL,
      method: "POST",
      data: {
        userID:getApp().getRandomString(10),
        userName: this.data.userName,
        passWord: this.data.password,
        dataTime: getApp().getNowDate()
      },
      success: function(res){
        console.log(res)
        if(res.data.status=="failed"){
          if(that.data.isRegister){
            that.setData({
              userName: '',
              password: '',
              isRegister: !that.data.isRegister
            })
            wx.showToast({
              title: '注册失败，请重试',
              icon: 'none'
            })
          }
          else{
            that.setData({
              userName: '',
              password: '',
              isRegister: that.data.isRegister
            })
            wx.showToast({
              title: '登录失败，请重试',
              icon: 'none'
            })
          }
        }
        else if(res.data.status == "success"){
          wx.setStorageSync('token', res.data.data )
          if(that.data.isRegister) {
            that.setData({
              userName: '',
              password: '',
              isRegister: !that.data.isRegister
            })
            wx.showToast({
              title: '注册成功，请登录',
              icon: 'none'
            })
          }else {
            wx.showToast({
              title: '登录成功',
              icon: 'none'
            })
            setTimeout(() => {
              wx.switchTab({
                url: '/pages/index/index',
              })     
            }, 2000);
          }
        }
      }
    })
  },
  getInputVal: function(e) {
    const type = e.currentTarget.dataset.type
    if (type == 'account') {
      this.setData({
        userName: e.detail.value
      })
    } else if (type == 'password') {
      this.setData({
        password: e.detail.value
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  goBack(){
    wx.switchTab({
      url: '../index/index',
    })
  }
})